package com.huohuzhihui.client;

import com.huohuzhihui.config.ContractConfig;
import com.huohuzhihui.contract.DigitalAsset;
import com.huohuzhihui.contract.DigitalAssetShard;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.fisco.bcos.sdk.abi.datatypes.generated.tuples.generated.Tuple7;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.model.TransactionReceipt;
import org.fisco.bcos.sdk.transaction.manager.AssembleTransactionProcessor;
import org.fisco.bcos.sdk.transaction.manager.TransactionProcessorFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.math.BigInteger;
import java.util.List;

@Service
@NoArgsConstructor
@Data
public class DigitalAssetShardClient {

    static Logger logger = LoggerFactory.getLogger(DigitalAssetShardClient.class);

    @Autowired
    private Client client;

    private String address = "0x82f51e9be11cbe65c835bd99753e4875fe6794c0";

    @Autowired
    private ContractConfig contractConfig;

    AssembleTransactionProcessor txProcessor;


    @PostConstruct
    public void init() throws Exception {
        this.txProcessor =
                TransactionProcessorFactory.createAssembleTransactionProcessor(
                        this.client, this.client.getCryptoSuite().getCryptoKeyPair());
    }



    public String add(String shard_id, String asset_id,
                 String from_address, String to_address, String transfer_type, String create_time) {
        try {

          DigitalAssetShard shard = DigitalAssetShard.load(this.address, client, this.client.getCryptoSuite().getCryptoKeyPair());
            TransactionReceipt receipt = shard.add( shard_id,  asset_id,  from_address,  to_address,  transfer_type,  create_time);

            return receipt.getTransactionHash();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            // e.printStackTrace();
            e.printStackTrace();
            logger.error(" registerAssetAccount exception, error message is {}", e.getMessage());
            System.out.printf(" register asset account failed, error message is %s\n", e.getMessage());
        }
        return null;
    }


}
